package com.huayi.website.controller.manage;

import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 登录拦截器
 */
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        HttpSession session = request.getSession();

//        HttpSession session = SessionHelper.getSession(request);
        /**当前访问的页面*/
        String targetUrl = request.getRequestURL().toString();
        if (StringUtils.isEmpty(request.getQueryString()) == false) {
            targetUrl += "?" + request.getQueryString();
        }

        if (session.getAttribute("userId") == null) {
            response.sendRedirect(request.getContextPath() + "/manage/login?ref=" + targetUrl);
//            response.sendRedirect(request.getContextPath() + "/login");
            return false;
        } else {
//            session.setAttribute("userId", session.getAttribute("userId"));
            return true;
        }
    }
}
